package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.data.model.AddressModel;
import com.kbbywyclient.data.model.BasicInfoModel;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.ModifyInfoContarct;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.customservice.ApiException;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/4/28
 */

public class ModifyInfoPresenter extends BasePresenter implements ModifyInfoContarct.Presenter {
    private ModifyInfoContarct.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    //为注解了@Inject的对象注入无参构造方法构造的对象
    @Inject
    public ModifyInfoPresenter(ModifyInfoContarct.View view) {
        this.view = view;

    }


    @Override
    public void getSalesInfo(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getSalesInfo(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BasicInfoModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        if (e instanceof ApiException) {

                        } else {
                            HttpExcUtil.printError(e);
                        }

                    }

                    @Override
                    public void onNext(BasicInfoModel basicInfoModel) {
                        view.loadSucBasicInfo(basicInfoModel.getData());
                    }
                });
    }

    @Override
    public void getAddress(String provinceID, String cityID, int type) {
        if (!provinceID.equals("")) {
            rsaJsonUtil.put("provinceID", provinceID);
        }
        if (!cityID.equals("")) {
            rsaJsonUtil.put("cityID", cityID);
        }
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
//        apiService.getAddress(provinceID, cityID)
        ApiServiceHelper.
                appointThreadNormal(apiService.getAddress(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<AddressModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(AddressModel addressModel) {
                        view.loadSucAdress(addressModel.getMsg().getData(), type);
                    }
                });

    }

    @Override
    public void saveModifyInfo(String SalesID, String TrueName, String Sex, String Position, String UserType
            , String CompanyName, String Province, String City, String Dist, String QQ) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("TrueName", TrueName);
        rsaJsonUtil.put("Sex", Sex);
        rsaJsonUtil.put("Position", Position);
        rsaJsonUtil.put("UserType", UserType);
        rsaJsonUtil.put("CompanyName", CompanyName);
        rsaJsonUtil.put("Province", Province);
        rsaJsonUtil.put("City", City);
        rsaJsonUtil.put("Dist", Dist);
        if (StringUtil.isNotEmpty(QQ)) {
            rsaJsonUtil.put("QQ", QQ);
        }
//        if (StringUtil.isNotEmpty(CompanyURL)) {
//            rsaJsonUtil.put("CompanyURL", CompanyURL);
//        }
//        if (StringUtil.isNotEmpty(RecommendMobile)) {
//            rsaJsonUtil.put("RecommendMobile", RecommendMobile);
//        }
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.saveModifyInfo(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BaseModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(BaseModel baseModel) {
                        view.loadSucSave(baseModel);
                    }
                });
    }

}
